<?php
/**
 * Created by PhpStorm
 * User: ZhouFurong
 * Date: 2020/12/2
 * Time: 17:03
 */


function test1()
{
    for ($i = 0; $i < 3; $i++) {
        yield $i + 1;
    }

    yield 1000;
    yield 1001;
}


//foreach (test1() as $t) {
//    echo $t, PHP_EOL;
//}


function test2()
{
    for ($i = 0; $i < 10; $i++) {
        if ($i == 4) {
            yield;
        }
        if ($i == 7) {
            return;
        }
        yield $i + 1;
    }
}

// 1,2,3,4 6,6
//foreach (test2() as $t) {
//    echo $t,PHP_EOL;
//}

function test3()
{
    for ($i = 0; $i < 10; $i++) {
        $data = (yield $i + 1);
        if ($data == 'stop') {
            return;
        }
    }
}

// send 方法 可以从外部传送一个 参数进来
//foreach (test3() as $t) {
//    if ($t == 3) {
//        $t->send('stop');
//    }
//    echo $t, PHP_EOL;
//}
//1
//2

// yield from 语法其实就是指的从另一个可迭代对象中一个一个的获取数据并形成生成器返回。直接看代码。
function test4()
{
    yield from [1, 2, 3, 4];
    yield from new ArrayIterator([5, 6]);
    yield from test1();
}

foreach (test4() as $t) {
    echo 'test4: ', $t, PHP_EOL;
}

function test5(){

}


